package pl.javarsovia.javarsovia4android.util;

import pl.javarsovia.R;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author mgrzechocinski
 * @since 2010-06-13
 */
public class UIUtils {

	public static void fillTextView(Activity activity, int viewResId, String textToSet) {
		TextView textView = (TextView) activity.findViewById(viewResId);
		setTextAndVisibility(textToSet, textView);
	}

	public static void fillTextView(View view, int viewResId, String textToSet) {
		TextView textView = (TextView) view.findViewById(viewResId);
		setTextAndVisibility(textToSet, textView);
	}

	private static void setTextAndVisibility(String textToSet, TextView textView) {
		// Do not overwrite default 'Brak' label
		if (TextUtils.isEmpty(textToSet) && defaultLabelExists(textView)) {
			return;
		}
		textView.setText(textToSet);
	}

	private static boolean defaultLabelExists(TextView textView) {
		return textView.getResources().getString(R.string.label_unknown).equals(textView.getText());
	}

	public static void fillImageView(View view, int id, Drawable image) {
		ImageView imageView = (ImageView) view.findViewById(id);
		imageView.setImageDrawable(image);
	}

	public static void fillImageView(Activity activity, int id, Drawable image) {
		ImageView imageView = (ImageView) activity.findViewById(id);
		imageView.setImageDrawable(image);
	}
}
